package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.app.dao.BankCardsDao;
import io.renren.modules.app.entity.BankCardsEntity;
import io.renren.modules.app.service.BankCardsService;
import org.springframework.transaction.annotation.Transactional;


@Service("bankCardsService")
public class BankCardsServiceImpl extends ServiceImpl<BankCardsDao, BankCardsEntity> implements BankCardsService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BankCardsEntity> page = this.page(
                new Query<BankCardsEntity>().getPage(params),
                new QueryWrapper<BankCardsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public BankCardsEntity queryUserBankCard(Long userId) {
        QueryWrapper<BankCardsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.last("limit 1");
        return baseMapper.selectOne(wrapper);
    }

    @Transactional
    @Override
    public Integer addBankCards(BankCardsEntity bankCards) {
        return baseMapper.insert(bankCards);
    }

    @Transactional
    @Override
    public Integer editBankCards(BankCardsEntity bankCards) {
        UpdateWrapper<BankCardsEntity> wrapper = new UpdateWrapper<>();
        if (bankCards.getUserId() != null) {
            wrapper.eq("user_id", bankCards.getUserId());
        }
        return baseMapper.update(bankCards, wrapper);
    }

}